const fs = require("fs");
const Docx = require("docx");
const fontkit = require("fontkit");
const docx = new Docx();
const file = fs.readFileSync("example.docx");
docx.load(file);
const paragraphs = docx.getParagraphs();
paragraphs.forEach((paragraph) => {
  const runs = paragraph.getRuns();
  runs.forEach((run) => {
    const font = fontkit.openSync(run.getFont().getFontPath());
    console.log(`Text: ${run.getText()}, Font: ${font.familyName}`);
  });
});



// --------------------------------------------------------
//在代码中，我们首先定义了一个字体识别器，它会在解析文档时被调用。然后我们加载doc文件，将字体识别器附加到docxtemplater实例中，并调用render方法来解析文档。最后，我们将结果输出到一个新的doc文件中。

const Docxtemplater = require('docxtemplater');
const FontsModule = require('docxtemplater-fonts');

// 定义字体识别器
const fonts = new FontsModule();
fonts.getFont = function(fontFamily, fontWeight, fontFile) {
  console.log(`Font family: ${fontFamily}, font weight: ${fontWeight}, font file: ${fontFile}`);
  // 在这里可以对字体进行处理
  return { family: fontFamily, bold: fontWeight === 'bold', italic: fontWeight === 'italic' };
};

// 加载doc文件
const fs = require('fs');
const buffer = fs.readFileSync('path/to/document.docx');

// 解析doc文件
const doc = new Docxtemplater();
doc.loadZip(buffer);
doc.attachModule(fonts);
doc.render();

// 输出结果
const result = doc.getZip().generate({ type: 'nodebuffer' });
fs.writeFileSync('path/to/output.docx', result);
